﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;
using MS.InformationManagement.Support.Constants;

namespace MS.InformationManagement.BLL.ReportesPrestamos
{
    public class ReportesPrestamos
    {

        public static List<SolicitudDePrestamosContentType> ObtenerMisPrestamos(Guid targetList)
        {
            List<SolicitudDePrestamosContentType> listResult = new List<SolicitudDePrestamosContentType>();

            SPList currentList = SPContext.Current.Web.Lists[targetList];
            SPUser user = SPContext.Current.Web.EnsureUser(currentList.Title);
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                          <And>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_USER_SOLICITUD + @"' LookupId= 'TRUE'/>
                                <Value Type='User'>" + user.ID + @"</Value>
                             </Eq>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO + @"' />
                                <Value Type='Choice'>" + EstadoDePrestamo.Aceptado + @"</Value>
                             </Eq>
                          </And>
                       </Where>";

            listResult = MS.InformationManagement.BLL.SolicitudesPrestamo.SolicitudesPrestamo.CargarSolicitudes(query, user);

            return listResult;
        }

        public static List<SolicitudDePrestamosContentType> ObtenerPrestamosDevueltos(Guid targetList)
        {
            List<SolicitudDePrestamosContentType> listResult = new List<SolicitudDePrestamosContentType>();

            SPList currentList = SPContext.Current.Web.Lists[targetList];
            SPUser user = SPContext.Current.Web.EnsureUser(currentList.Title);
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                          <And>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_USER_SOLICITUD + @"' LookupId= 'TRUE'/>
                                <Value Type='User'>" + user.ID + @"</Value>
                             </Eq>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO + @"' />
                                <Value Type='Choice'>" + EstadoDePrestamo.Devuelto + @"</Value>
                             </Eq>
                          </And>
                       </Where>";

            listResult = MS.InformationManagement.BLL.SolicitudesPrestamo.SolicitudesPrestamo.CargarSolicitudes( query, user);

            return listResult;
        }

        public static List<SolicitudDePrestamosContentType> ObtenerPrestamosRechazados(Guid targetList)
        {
            List<SolicitudDePrestamosContentType> listResult = new List<SolicitudDePrestamosContentType>();

            SPList currentList = SPContext.Current.Web.Lists[targetList];
            SPUser user = SPContext.Current.Web.EnsureUser(currentList.Title);
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                          <And>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_USER_PRESTAMO + @"' LookupId= 'TRUE'/>
                                <Value Type='User'>" + user.ID + @"</Value>
                             </Eq>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO + @"' />
                                <Value Type='Choice'>" + EstadoDePrestamo.Rechazado + @"</Value>
                             </Eq>
                          </And>
                       </Where>";

            listResult = MS.InformationManagement.BLL.SolicitudesPrestamo.SolicitudesPrestamo.CargarSolicitudes( query, user);

            return listResult;
        }

        public static List<SolicitudDePrestamosContentType> ObtenerSolicitudesRechazadas(Guid targetList)
        {
            List<SolicitudDePrestamosContentType> listResult = new List<SolicitudDePrestamosContentType>();

            SPList currentList = SPContext.Current.Web.Lists[targetList];
            SPUser user = SPContext.Current.Web.EnsureUser(currentList.Title);
            SPQuery query = new SPQuery();
            query.Query = @"<Where>
                          <And>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_USER_SOLICITUD + @"' LookupId= 'TRUE'/>
                                <Value Type='User'>" + user.ID + @"</Value>
                             </Eq>
                             <Eq>
                                <FieldRef Name='" + CustomSiteColumns.FIELD_SOLICITUD_PRESTAMOS_ESTADO + @"' />
                                <Value Type='Choice'>" + EstadoDePrestamo.Rechazado + @"</Value>
                             </Eq>
                          </And>
                       </Where>";


            listResult = MS.InformationManagement.BLL.SolicitudesPrestamo.SolicitudesPrestamo.CargarSolicitudes( query, user);

            return listResult;
        }
    }
}
